package io.renren.modules.eightapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.eightapp.dao.EiParkInfoDao;
import io.renren.modules.eightapp.dao.EiParkingInfoDao;
import io.renren.modules.eightapp.dao.EiUseparkingInfoDao;
import io.renren.modules.eightapp.dto.EiParkInfoDTO;
import io.renren.modules.eightapp.dto.EiUseparkingInfoDTO;
import io.renren.modules.eightapp.entity.EiParkInfoEntity;
import io.renren.modules.eightapp.entity.EiParkingInfoEntity;
import io.renren.modules.eightapp.entity.EiUseparkingInfoEntity;
import io.renren.modules.eightapp.service.EiUseparkingInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 
 *
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-29
 */
@Service
public class EiUseparkingInfoServiceImpl extends CrudServiceImpl<EiUseparkingInfoDao, EiUseparkingInfoEntity, EiUseparkingInfoDTO> implements EiUseparkingInfoService {
    @Autowired
    private EiUseparkingInfoDao eiUseparkingInfoDao;
    @Autowired
    private EiParkingInfoDao eiParkingInfoDao;
    @Autowired
    private EiParkInfoDao eiParkInfoDao;


    @Override
    public QueryWrapper<EiUseparkingInfoEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<EiUseparkingInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void savePark(EiUseparkingInfoDTO dto) {
        dto.setCreator("Test");
        dto.setCreateTime(new Date(11111111111111L));
        dto .setUpdateTime(new Date());
        dto .setStarttime(new Date());
        EiUseparkingInfoEntity eiUseparkingInfoEntity=new EiUseparkingInfoEntity();
        BeanUtils.copyProperties(dto,eiUseparkingInfoEntity);
        //插入使用信息表
        int insert = eiUseparkingInfoDao.insert(eiUseparkingInfoEntity);
        if (insert>0){
            //停车场表使用加一
            this.updateParkCount(0,eiUseparkingInfoEntity.getParkId());
            //修改停车位状态
            this.updateParkingstate(1,eiUseparkingInfoEntity.getParkId(),eiUseparkingInfoEntity.getParkingId());
        }
    }

    @Override
    public void deletePark(Integer id) {
        //删除使用信息表
        EiUseparkingInfoEntity eiUseparkingInfoEntity = eiUseparkingInfoDao.selectById(id);

        int insert = eiUseparkingInfoDao.deleteById(id);
        if (insert>0){
            //停车场表使用减一
            this.updateParkCount(1,eiUseparkingInfoEntity.getParkId());
            //修改停车位状态
            this.updateParkingstate(0,eiUseparkingInfoEntity.getParkId(),eiUseparkingInfoEntity.getParkingId());
        }
    }



    /**
     * 停车场表使用
     * @param type 加:0  减:1
     * @param parkid
     */
    public void updateParkCount(int type,Integer parkid){
        EiParkInfoEntity eiParkInfoEntity = eiParkInfoDao.selectById(parkid);
        if (type==0){
            eiParkInfoEntity.setParkuseCount(eiParkInfoEntity.getParkuseCount()+1);
        }
        else {
            eiParkInfoEntity.setParkuseCount(eiParkInfoEntity.getParkuseCount()-1);
        }
        eiParkInfoDao.updateById(eiParkInfoEntity);
    }

    /**
     * 修改停车位状态
     * @param state 0没被租用 1被租用
     * @param parkid
     */
    public void updateParkingstate(int state,Integer parkid,Integer parkingid){
        EiParkingInfoEntity eiParkingInfoEntity=new EiParkingInfoEntity();
        eiParkingInfoEntity.setState(state);
        UpdateWrapper<EiParkingInfoEntity> eiParkingInfoEntityUpdateWrapper=new UpdateWrapper<>();
        eiParkingInfoEntityUpdateWrapper.lambda().eq(EiParkingInfoEntity::getParkId,parkid).eq(EiParkingInfoEntity::getParkingId,parkingid);
        eiParkingInfoDao.update(eiParkingInfoEntity,eiParkingInfoEntityUpdateWrapper);
    }


}